﻿using NB.MGJ.Common.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Log
{
    public static class LoggerManager
    {
        #region 对象变量

        private static Dictionary<string, ILoggerProvider> logProviders;

        private static string[] loggerNames = new string[] { "SystemExceptionLogger", "UserExceptionLogger", "ControllerActionLogger", "CustomLogger" };

        private static ILoggerProvider _systemExceptionLogger { get { return logProviders["SystemExceptionLogger"]; } }

        private static ILoggerProvider _userExceptionLogger { get { return logProviders["UserExceptionLogger"]; } }

        private static ILoggerProvider _controllerActionLogger { get { return logProviders["ControllerActionLogger"]; } }

        private static ILoggerProvider _customLogger { get { return logProviders["CustomLogger"]; } }

        #endregion 对象变量

        #region 初始化

        /// <summary>
        /// 静态类构造函数
        /// </summary>
        static LoggerManager()
        {
            logProviders = new Dictionary<string, ILoggerProvider>();

            Initialize();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialize()
        {
            logProviders.Clear();

            InitializeLoggerProvider();
        }

        private static void InitializeLoggerProvider()
        {
            foreach (var item in loggerNames)
            {
                logProviders.Add(item, new Log4NetProvider(item));
            }
        }

        #endregion 初始化

        #region ControllerAction相关
        /// <summary>
        /// Action进入信息记录
        /// </summary>
        /// <param name="info"></param>
        public static void Start(string info)
        {
            //if (EnvironmentHandle.GlobalSettings.LogSetting.ActionLog)
            //{
            //    _controllerActionLogger.Info(info);
            //}
        }

        /// <summary>
        /// Action进入信息记录
        /// </summary>
        /// <param name="model"></param>
        //public static void Start(ControllerActionLogModel model)
        //{
        //    //if (Environment.EnvironmentHandle.GlobalSettings.LogSetting.ActionLog)
        //    //{
        //    //    _controllerActionLogger.Info(model.ToShortString());
        //    //}
        //}

        /// <summary>
        /// Action结束信息记录
        /// </summary>
        /// <param name="info"></param>
        public static void End(string info)
        {
            //if (Environment.EnvironmentHandle.GlobalSettings.LogSetting.ActionLog)
            //{
            //    _controllerActionLogger.Info(info);
            //}
        }

        /// <summary>
        /// Action结束信息记录
        /// </summary>
        /// <param name="model"></param>
        //public static void End(ControllerActionLogModel model)
        //{
        //    //if (Environment.EnvironmentHandle.GlobalSettings.LogSetting.ActionLog)
        //    //{
        //    //    _controllerActionLogger.Info(model.ToShortString());
        //    //}
        //}

        #endregion ControllerAction相关

        #region 异常记录

        /// <summary>
        /// 记录系统异常
        /// </summary>
        /// <param name="ex"></param>
        public static void LogSysException(System.Exception ex)
        {
            _systemExceptionLogger.Fatal("系统异常", ex);
        }

        /// <summary>
        /// 记录用户异常
        /// </summary>
        /// <param name="ex"></param>
        public static void LogUserException(UserException ex)
        {
            _userExceptionLogger.Error(ex.Message,ex);
        }

        #endregion 异常记录

        #region 自定义信息记录
        /// <summary>
        /// 记录自定义信息
        /// </summary>
        /// <param name="message"></param>
        public static void LogCustomInfo(string message)
        {
            _customLogger.Info(message);
        }
        #endregion 自定义信息记录
    }

    /// <summary>
    /// 用来描述一个类的属性
    /// </summary>
    public class ClassProperty
    {
        public ClassProperty()
        {

        }
        public string Name { get; set; }
        public string ValueString { get; set; }
        public object Value { get; set; }
        public string DisplayName { get; set; }
        public string PropertyType { get; set; }
        public bool IsValue { get; set; }
    }
}
